//引入express模块
const express = require('express')
//引用数据库连接池
const pool = require('../pool.js')
//创建路由器
const ulr = express.Router()

//页面地址 http://127.0.0.1:8080/userlist.html

//请求方式 get
//路由地址  127.0.0.1:8080/userlist/selectAll
//接收传参的方法 pool.query
ulr.get('/selectAll',(req,res)=>{
    console.log(req.query)
    pool.query('select * from users',(err,data)=>{
        if(err){
            next(err)
            return
        }
        // console.log(data)
        
        //判断是否查询到数据
        if(data.length == 0){
            res.send({
                "code":0,
                "msg":"没有找到"
            })
        }else{
            res.send({
                "code": 1,
                "msg": "找到用户信息",
                "data":data
            })
        }
    })
})


ulr.get('/getuser',(req,res)=>{
    let obj = parseInt(req.query.uid)
    // console.log(req.query)
    pool.query('select * from users where uid=?',[obj],(err,data)=>{
        if(err){
            next(err)
            return
        }
        // console.log(data)
        
        //判断是否查询到数据
        if(data.length == 0){
            res.send({
                "code":0,
                "msg":"没有找到"
            })
        }else{
            res.send({
                "code": 1,
                "msg": "找到用户信息",
                "data":data
            })
        }
    })
})

ulr.post('/changeuser',(req,res,next)=>{
    console.log(req.body)
    let bid = parseInt(req.body.cid)
    let bname = req.body.cname
    let bpassword = req.body.cpassword
    let bnum = req.body.cnumber
    let bbirth = req.body.cbirth
    let sql = `update users set uname=?,unumber=?,upassword=?,ubirthday=? where uid=?`
    pool.query(sql,[bname,bnum,bpassword,bbirth,bid],(err,data)=>{
        if(err){
            next(err)
            return
        }
        // console.log(data)
        
        //判断是否查询到数据
        if(data.length == 0){
            res.send({
                "code":0,
                "msg":"修改失败"
            })
        }else{
            res.send({
                "code": 1,
                "msg": "修改成功",
                "data":data
            })
        }
    })
})

module.exports = ulr